<template>
  <el-card class="card-box">
    <el-row slot="header" type="flex" class="clearfix">
      <el-col>
        <el-button v-if="checkPermBtn('arceAdd')" type="primary" size="small" @click="showDialog=true">添加房产</el-button>
      </el-col>
      <el-col>
        <el-row type="flex">
          <el-select v-model="agent" placeholder="请选择经纪" @change="search">
            <el-option label="全部经纪" :value="null" />
            <el-option v-for="item in agentList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
          <el-select v-model="category" placeholder="请选择类别" @change="search">
            <el-option label="全部分类" :value="null" />
            <el-option v-for="item in categoryList" :key="item.id" :label="item.title" :value="item.id" />
          </el-select>
          <el-input v-model="title_contains" placeholder="请输入关键字" @input="search" />
          <el-button size="small" @click="search">筛选</el-button>
        </el-row>
      </el-col>
    </el-row>
    <el-table border :data="formData">
      <el-table-column
        prop="image"
        label="封面"
      >
        <template v-slot="{row}">
          <img :src="row.image" width="70px">
        </template>
      </el-table-column>
      <el-table-column
        prop="title"
        label="标题"
      />
      <el-table-column
        prop="tags"
        label="标签"
      >
        <template v-slot="{row}">
          <el-tag v-for="item in row.tags" :key="item" type="success">{{ item }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="address"
        label="地址"
      />
      <el-table-column
        prop="category.title"
        label="分类"
      />
      <el-table-column
        prop="agent.name"
        label="经纪"
      />
      <el-table-column
        prop="price"
        label="价格"
      />
      <el-table-column
        prop="desc"
        label="介绍"
      />
      <el-table-column
        prop="photos"
        label="图片"
      >
        <template v-slot="{row}">
          <template v-if="typeof(row.photos)==='string'">
            <img :src="row.photos" width="70px">
          </template>
          <template v-else>
            <img v-for="item in row.photos" :key="item" :src="item" width="70px">
          </template>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="150"
      >
        <template v-slot="{row}">
          <el-button v-if="checkPermBtn('arceEdit')" type="primary" size="small" @click="editFn(row.id)">编辑</el-button>
          <el-button v-if="checkPermBtn('arceDel')" type="danger" size="small" @click="delFn(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-row type="flex" justify="center">
      <el-pagination
        layout="prev, pager, next"
        :page-size="limit"
        :total="total"
        @current-change="pageChange"
      />
    </el-row>
    <AddProperties
      ref="addProperties"
      :show-dialog.sync="showDialog"
      :agent-list="agentList"
      :category-list="categoryList"
      @updateFn="getProperties"
    />
  </el-card>
</template>

<script>
import { getPropertiesList, delProperties, getProCount, getAgentList, getCategoryList } from '../../api/properties'
import AddProperties from './components/add-properties.vue'
export default {
  components: { AddProperties },
  data() {
    return {
      formData: [],
      limit: 2,
      start: 0,
      showDialog: false,
      total: 0,
      agent: null,
      category: null,
      title_contains: null,
      agentList: [], // 经纪下拉列表
      categoryList: [] // 分类下拉列表
    }
  },
  async created() {
    this.getProperties()
    this.getTotal()
    this.getAgent()
    this.getCategory()
  },
  methods: {
    // 获取房产数量
    async getTotal() {
      const res = await getProCount({
        title_contains: this.title_contains,
        agent: this.agent,
        category: this.category
      })
      this.total = res.data
      // console.log('total', this.total)
    },
    async editFn(id) {
      // const res = await getPropertiesById(id)
      // console.log('房产', res)
      // this.$refs.addProperties.formData = res.data
      this.$refs.addProperties.getInfo(id)
      this.showDialog = true
    },
    async delFn(id) {
      await delProperties(id)
      this.$message({
        message: '删除成功',
        type: 'success'
      })
      this.getProperties()
      // console.log(id)
    },
    pageChange(page) {
      this.start = (page - 1) * this.limit
      this.getProperties()
    },
    // 搜索
    async search() {
      const res = await getPropertiesList({
        _limit: this.limit,
        title_contains: this.title_contains,
        agent: this.agent,
        category: this.category
      })
      // console.log(res)
      this.formData = res.data
      this.getTotal()
    },
    async getProperties() {
      const res = await getPropertiesList({
        _limit: this.limit,
        _start: this.start,
        title_contains: this.title_contains,
        agent: this.agent,
        category: this.category
      })
      // console.log(res)
      this.formData = res.data
      this.getTotal()
      // console.log('total', this.total)
    },
    async getAgent() {
      const res = await getAgentList()
      this.agentList = res.data
    },
    async getCategory() {
      const res = await getCategoryList()
      this.categoryList = res.data
    }
  }
}
</script>

<style lang="scss" scoped>
.card-box{
  margin: 20px;
}
</style>
